/**
 * Created with IntelliJ IDEA.
 * Description:实现二叉搜索树
 * Date: 2024-02-09
 * Time:12:34
 */
public class BinarySearchTree {
    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "key=" + key +
                    '}';
        }
    }

    public TreeNode root;



    //插入一个关键字key
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);
        if(root == null) {
            root = node;
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            if(cur.key < key) {
                parent = cur;
                cur = cur.right;
            } else if(cur.key > key) {
                parent = cur;
                cur = cur.left;
            } else {
                return false;
            }
        }
        if(key > parent.key)
            parent.right = node;
        if(key < parent.key)
            parent.left = node;
        return true;
    }

    //查找key是否存在
    public TreeNode search(int key) {
        if(root == null)
            return null;
        TreeNode cur = root;
        while(cur != null) {
            if(cur.key < key) {
                cur = cur.right;
            } else if(cur.key > key) {
                cur = cur.left;
            } else {
                return cur;
            }
        }
            return null;  //到这里说明找不到，返回null
    }

    //删除key的值
    public boolean remove(int key) {
        if(root == null)
            return false;
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            if(cur.key < key) {
                parent = cur;
                cur = cur.right;
            } else if(cur.key > key) {
                parent = cur;
                cur = cur.left;
            } else {  //找到cur了，准备把它删了
                if(cur.left == null) {
                    if(cur == root) {
                        root = root.right;
                        return true;
                    } else {
                        if(cur == parent.left)
                            parent.left = cur.right;
                        if(cur == parent.right)
                            parent.right = cur.right;
                    }
                } else if(cur.right == null) {
                    if(cur == root) {
                        root = root.left;
                        return true;
                    } else {
                        if(cur == parent.left)
                            parent.left = cur.left;
                        if(cur == parent.right)
                            parent.right = cur.left;
                    }
                } else {  //左右都不为空
                    TreeNode target = cur.right;  //让target去右子树找到最左边的节点
                    TreeNode targetParent = cur;
                    while(target.left != null) {
                        targetParent = target;
                        target = target.left;
                    }
                    //将tmp的关键字赋给cur
                    cur.key = target.key;
                    //删除tmp节点
                    if(targetParent.left == target) {
                        targetParent.left = cur.right;
                    } else {
                        targetParent.right = cur.right;
                    }
                }
                return true;
            }
        }
        return false;
    }
}

